package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.FindDetailsActivity;
import com.zzk.lanouhn.myapplication1.activity.FindDetailsXiaoTuActivity;
import com.zzk.lanouhn.myapplication1.modles.FindModle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/1.
 */
public class FindAdapter extends RecyclerView.Adapter<FindAdapter.MyViewHolder> {
    private Context mContext;
    private List<FindModle> findModles = new ArrayList<>();

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public FindAdapter(Context mContext, List<FindModle> findModles) {
        this.mContext = mContext;
        this.findModles = findModles;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //找布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_find, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.tv_zhuti.setText(findModles.get(position).getName());
        holder.tv_shiJian.setText(findModles.get(position).getStart_date());
        holder.tv_tianShu.setText(findModles.get(position).getDays() + "天");
        holder.tv_tuShu.setText(findModles.get(position).getPhotos_count() + "图");

        Picasso.with(mContext).load(findModles.get(position).getFront_cover_photo_url()).into(holder.iv_daTu);
        Picasso.with(mContext).load(findModles.get(position).getUser().getImage()).into(holder.iv_xiaoTu);

        //大图点击事件，带值跳转
        holder.iv_daTu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = findModles.get(position).getId();
                String string=Integer.toString(id);
                Intent intent=new Intent(mContext, FindDetailsActivity.class);
                intent.putExtra("id",string);
                mContext.startActivity(intent);
            }
        });

        sp=mContext.getSharedPreferences("config",mContext.MODE_PRIVATE);
        editor=sp.edit();

        //小图点击事件，带值跳转
        holder.iv_xiaoTu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int xiaotuID=findModles.get(position).getUser().getId();
                String xID=Integer.toString(xiaotuID);
                Intent intent=new Intent(mContext, FindDetailsXiaoTuActivity.class);
                intent.putExtra("xt_id",xID);
                //郊游的id和like的ID
                editor.putString("xiaotu_youji",xID);
                //提交
                editor.commit();
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return findModles.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_daTu, iv_xiaoTu;
        private TextView tv_zhuti, tv_shiJian, tv_tianShu, tv_tuShu;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_daTu = (ImageView) itemView.findViewById(R.id.find_iv_da);
            iv_xiaoTu = (ImageView) itemView.findViewById(R.id.find_iv_xiaotu);
            tv_shiJian = (TextView) itemView.findViewById(R.id.find_tv_shijian);
            tv_tianShu = (TextView) itemView.findViewById(R.id.find_tv_tianshu);
            tv_tuShu = (TextView) itemView.findViewById(R.id.find_tv_tushu);
            tv_zhuti = (TextView) itemView.findViewById(R.id.find_tv_zhuti);
        }
    }

}
